{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 统计查找替换类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "s='hello world hello python'"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24\n"
     ]
    }
   ],
   "source": [
    "#len获取字符串长度\n",
    "print(len(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "#count(str,__start,__end) 方法返回str在string中出现的次数,start end 为查找范围开始位置和结束位置\n",
    "print(s.count('o'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "14\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp\\ipykernel_8908\\1208636976.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ms\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mindex\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'll'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ms\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mindex\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'll'\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;36m3\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 4\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0ms\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mindex\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'Python'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m: substring not found"
     ]
    }
   ],
   "source": [
    "#index(str,start,end)  返回找到第一次找到的索引值start和end还是范围\n",
    "print(s.index('ll'))\n",
    "print(s.index('ll',3))\n",
    "print(s.index('Python'))  #找不到就报错"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n"
     ]
    }
   ],
   "source": [
    "#rindex(str,start,end)  从右侧查找  返回找到第一次找到的索引值start和end还是范围\n",
    "print(s.rindex('ll'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "#find() rfind()方法用法同上，找不到返回-1\n",
    "print(s.find('ll'))\n",
    "print(s.find('lll'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "heLLo world hello python\n"
     ]
    }
   ],
   "source": [
    "#replace(old,new,max) 字符串替换方法  old旧字符串   new新字符串   max最多替换max次\n",
    "print(s.replace('ll', 'LL',1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字符串判断类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "url='https://www.ceshiren.com'"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "#startswith()方法判断是否以指定字符串开头\n",
    "print(url.startswith('http'))\n",
    "print(url.startswith('http',1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#endswith()判断是否以指定字符结尾\n",
    "print(url.endswith('.com'))\n",
    "print(url.endswith('.com',0,len(url)-1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 判断字符串是否全是字母，至少有一个字符    isalpha()\n",
    "print('aWDSwds'.isalpha())\n",
    "print('aw323ds'.isalpha())\n",
    "print('awd.,.,s'.isalpha())\n",
    "print('awds汉字'.isalpha())\n",
    "print('awds'.isalpha())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#判断是否全是数字  isdigit()\n",
    "print('12313'.isdigit())\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#判断字符串是否全空格、空白  isspace（） 注：空串时False\n",
    "print(' '.isspace())\n",
    "print('  '.isspace())\n",
    "print('dsd'.isspace())\n",
    "print('123'.isspace())\n",
    "print('/\\'.;,'.isspace())\n",
    "print(''.isspace())\n",
    "print('\\n'.isspace())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#判断字符串是否全大写字母  isupper()\n",
    "#判断是否小写字母  islower()\n",
    "print('WDS'.isupper())\n",
    "print('WDS1213'.isupper())\n",
    "print('WDS。、'.isupper())\n",
    "print('DSDV你好'.isupper())\n",
    "print('WDSwdsd'.isupper())\n",
    "print(''.isupper())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n",
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#判断是字符串中所有单词是否标题化  单词首字符大写 其他字母小写 istitle()\n",
    "print('User Name'.istitle())\n",
    "print('User_Name'.istitle())\n",
    "print('user name'.istitle())\n",
    "print('User name'.istitle())\n",
    "print('User_name'.istitle())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字符串转换类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Username\n",
      "Username\n",
      "Username\n",
      "Hello world\n"
     ]
    }
   ],
   "source": [
    "# 将字符串第一个首字母大写，其余全小写  capitalize（）\n",
    "print('Username'.capitalize())\n",
    "print('UserName'.capitalize())\n",
    "print('USERNAME'.capitalize())\n",
    "print('hello world'.capitalize())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Username\n",
      "User Name\n",
      "User Name\n",
      "User_Name\n"
     ]
    }
   ],
   "source": [
    "#将字符串标题话 title\n",
    "print('username'.title())\n",
    "print('user name'.title())\n",
    "print('USER NAME'.title())\n",
    "print('user_name'.title())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "# 将字符串中字母转换大写 upper"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "# 将字符串中字母转换小写  lower"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字符串对齐类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "center() takes at least 1 argument (0 given)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m~\\AppData\\Local\\Temp\\ipykernel_16180\\4082791528.py\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[1;31m# 字符串在指定宽度内居中显示  center(宽度,‘填充字符’)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[0ms\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;34m'hello'\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 3\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'|'\u001B[0m\u001B[1;33m+\u001B[0m\u001B[0ms\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mcenter\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'|'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      4\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;34m'|'\u001B[0m\u001B[1;33m+\u001B[0m\u001B[0ms\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mcenter\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;36m25\u001B[0m\u001B[1;33m,\u001B[0m\u001B[1;34m'.'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m+\u001B[0m\u001B[1;34m'|'\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mTypeError\u001B[0m: center() takes at least 1 argument (0 given)"
     ]
    }
   ],
   "source": [
    "# 字符串在指定宽度内居中显示  center(宽度,‘填充字符’)\n",
    "s = 'hello'\n",
    "print('|'+s.center(25)+'|')\n",
    "print('|'+s.center(25,'.')+'|')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|hello                    |\n",
      "|hello....................|\n"
     ]
    }
   ],
   "source": [
    "# 字符串在指定宽度内左对齐显示  ljust()\n",
    "print('|'+s.ljust(25)+'|')\n",
    "print('|'+s.ljust(25,'.')+'|')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|                    hello|\n",
      "|....................hello|\n"
     ]
    }
   ],
   "source": [
    "# 字符串在指定宽度内右对齐显示  rjust()\n",
    "print('|'+s.rjust(25)+'|')\n",
    "print('|'+s.rjust(25,'.')+'|')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 去除空白类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|大晚上的|\n"
     ]
    }
   ],
   "source": [
    "# 删除左右两侧空白 strip()\n",
    "print('|'+ '  大晚上的   '.strip()+'|')\n",
    "# 删除 左边空白  lstrip()\n",
    "# 删除 右边空白  rstrip()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字符串分割类"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd']\n",
      "['a', 'b', 'c-d']\n",
      "['a', 'b-c-d']\n"
     ]
    }
   ],
   "source": [
    "# 字符串分割方法  split('分隔符',分割次数)  分隔符默认为空白分割,分割后放在元组中\n",
    "s='a-b-c-d'\n",
    "print(s.split('-'))\n",
    "print(s.split('-',2))\n",
    "print('a--b-c-d'.split('--',2))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "# 使用换行符分割字符串  splitlines()\n",
    "print('a\\nb\\nc'.splitlines())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('http://', 'www', '.badu.com')\n",
      "('http://www.badu.com', '', '')\n",
      "('http://www.badu.', 'com', '')\n",
      "('', 'http', '://www.badu.com')\n"
     ]
    }
   ],
   "source": [
    "# 字符串分割为三段  partition('sep')    rpartition() 右\n",
    "print('http://www.badu.com'.partition('www'))\n",
    "print('http://www.badu.com'.partition('wwww'))\n",
    "print('http://www.badu.com'.partition('com'))\n",
    "print('http://www.badu.com'.partition('http'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字符串拼接"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h-e-l-l-o- -w-o-r-l-d\n",
      "a-b-c\n",
      "a-b-c\n",
      "a-b-c\n",
      "AA-BB-CC\n"
     ]
    }
   ],
   "source": [
    "# 加号拼接两个字符串\n",
    "\n",
    "# 乘号拼接字符串n次\n",
    "\n",
    "#join()方法使用字符串连接容器中字符串元素\n",
    "print('-'.join('hello world'))\n",
    "print('-'.join(('a','b','c')))\n",
    "print('-'.join(['a','b','c']))\n",
    "print('-'.join({'a':'AA','b':'BB','c':'CC'}))\n",
    "print('-'.join({'a':'AA','b':'BB','c':'CC'}.values()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 字符串编码方法"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xc4\\xe3\\xba\\xc3\\xb0\\xa1'\n",
      "b'\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd'\n"
     ]
    }
   ],
   "source": [
    "# encode 将字符串转 二进制\n",
    "print('你好啊'.encode('gbk'))\n",
    "print('你好'.encode('utf8'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好啊\n",
      "你好\n"
     ]
    }
   ],
   "source": [
    "# decode 将二进制转字符串\n",
    "print(b'\\xc4\\xe3\\xba\\xc3\\xb0\\xa1'.decode('gbk'))\n",
    "print(b'\\xe4\\xbd\\xa0\\xe5\\xa5\\xbd'.decode('utf8'))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 切片"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "02\n",
      "012\n",
      "54321\n",
      "012345\n"
     ]
    }
   ],
   "source": [
    "# 字符串切片 切片时公共操作不是字符串独有的  可迭代对象都可以进行切片操作 s[start:end:step]\n",
    "print('0123456789'[:3:2])\n",
    "print('0123456789'[:3])\n",
    "print('0123456789'[5:0:-1])\n",
    "print('0123456789'[:6])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}